import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';

class MyImage extends StatelessWidget {
  final double? width;
  final double? height;
  final String path;
  final Color? color;
  final double rotate;
  final BoxFit? fit;
  final bool showPlaceholder;
  final String? placeholderPath;
  final String? errorImagePath;
  final Animation<double>? controller;
  final bool? repeat;
  final bool? animate;
  final BorderRadius? borderRadius;
  final Widget Function()? builderPlaceHolder;
  final Widget Function()? builderError;

  const MyImage(
      {Key? key,
      this.width,
      this.height,
      this.color,
      this.rotate = 0,
      this.fit,
      this.borderRadius,
      this.showPlaceholder = false,
        this.placeholderPath,
        this.errorImagePath,
      this.controller,
      this.repeat,
      this.animate,
      this.builderPlaceHolder,
      this.builderError,
      required this.path})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Transform.rotate(
      angle: pi / (180 / rotate),
      child: ClipRRect(
        borderRadius: borderRadius ?? BorderRadius.zero,
        child: _buildImage(),
      ),
    );
  }

  _buildImage() {
    if(path.isEmpty){
      return _getImageError();
    }
    return _isNetwork() ? _buildNetworkImage() : _buildLocalImage();
  }

  bool _isNetwork() {
    return path.startsWith("http");
  }

  bool _isSvg() {
    return path.endsWith(".svg");
  }

  _getImagePlaceHolder() {
    String imagePath = 'assets/img/error_placeholder_pic.png';
    if(placeholderPath?.length != null){
      imagePath = placeholderPath!;
    }
    return builderPlaceHolder?.call() ??
        SizedBox(
          width: width,
          height: height,
          child: Image.asset(imagePath),
        );
  }

  _getImageError() {
    String imagePath = 'assets/img/error_placeholder_pic.png';
    if(placeholderPath?.length != null){
      imagePath = errorImagePath!;
    }
    return builderError?.call() ??
        SizedBox(
          width: width,
          height: height,
          child: Image.asset(imagePath),
        );
  }

  Widget _buildLocalImage() {
    if (_isSvg()) {
      return SvgPicture.asset(
        path,
        color: color,
        fit: fit ?? BoxFit.contain,
        width: width,
        height: height,
        placeholderBuilder:
            showPlaceholder ? (context) => _getImagePlaceHolder() : null,
      );
    } else {
      return Image.asset(
        path,
        color: color,
        fit: fit,
        errorBuilder: (context, error, stackTrace) => _getImageError(),
        width: width,
        height: height,
      );
    }
  }

  Widget _buildNetworkImage() {
    if (_isSvg()) {
      return SvgPicture.network(
        path,
        color: color,
        fit: fit ?? BoxFit.contain,
        width: width,
        height: height,
        placeholderBuilder:
            showPlaceholder ? (context) => _getImagePlaceHolder() : null,
      );
    } else {
      try {
        return CachedNetworkImage(
                color: color,
                width: width,
                height: height,
                // key: Key(path),
                imageUrl: path,
                fit: fit ?? BoxFit.cover,
                placeholder:
                    showPlaceholder ? (context, url) => _getImagePlaceHolder() : null,
                errorWidget: (context, url, error) => _getImageError(),
              );
      } catch (e) {
        return Image(
          image: AssetImage(path),
        );
      }
    }
  }
}
